package tools

import config.{Global, MyFile, MyRow}
import models.Tables.BlockrangeRow
import play.api.libs.json.Json
import utils.TableUtils

object Circos extends MyFile with MyRow {

  def circosPlot(species: String, chrS: String, species2: String, lines: Seq[BlockrangeRow]) = {

    val ref = s"${Global.path}/blockLen/$species.txt".readLines.map(_.split("\t").map(_.trim)).filter(_.head == chrS).zipWithIndex

    val refRow = ref.map { x =>
      val chr = x._1.head
      val length = x._1.last
      Json.obj("id" -> 1, "chr" -> chr, "length" -> length.toInt, "color" -> "red")
    }

    val query = s"${Global.path}/blockLen/$species2.txt".readLines.map(_.split("\t").map(_.trim)).zipWithIndex

    val queryRow = query.map { x =>
      val chr = x._1.head
      val length = x._1.last
      Json.obj("id" -> (x._2 + 2), "chr" -> chr, "length" -> length.toInt, "color" -> "blue")
    }

    val chr1 = query.map(x => x._1.head -> (x._2 + 2)).toMap

    val row = lines.zipWithIndex.map { case (y, i) =>
      if (y.querySpecies == species) {
        Json.obj("bid" -> y.name, "color" -> "black", "id" -> (i + 1),
          "source" -> Json.obj("chr" -> 1, "name" -> chrS, "start" -> y.queryStart, "end" -> y.queryEnd),
          "target" -> Json.obj("chr" -> chr1(y.ref), "name" -> y.ref, "start" -> y.refStart, "end" -> y.refEnd))
      } else {
        Json.obj("bid" -> y.name, "color" -> "black", "id" -> (i + 1),
          "source" -> Json.obj("chr" -> 1, "name" -> chrS, "start" -> y.refStart, "end" -> y.refEnd),
          "target" -> Json.obj("chr" -> chr1(y.query), "name" -> y.query, "start" -> y.queryStart, "end" -> y.queryEnd))
      }
    }
    val collection = queryRow ++ refRow
    val table = lines.map(z => z.getMapByT() ++ Map("qSpecies" -> getTitle(z.querySpecies), "rSpecies" -> getTitle(z.refSpecies)))
    Json.obj("ur" -> row, "collection" -> collection, "table" -> table)
  }

  def circosPlotAll(species: String,  species2: String, lines: Seq[BlockrangeRow]) = {

    val ref = s"${Global.path}/blockLen/$species.txt".readLines.map(_.split("\t").map(_.trim)).zipWithIndex

    val refRow = ref.map { x =>
      val chr = x._1.head
      val length = x._1.last
      Json.obj("id" -> (x._2 +1), "chr" -> chr, "length" -> length.toInt, "color" -> "red")
    }

    val query = s"${Global.path}/blockLen/$species2.txt".readLines.map(_.split("\t").map(_.trim)).zipWithIndex

    val queryRow = query.map { x =>
      val chr = x._1.head
      val length = x._1.last
      Json.obj("id" -> (x._2 + 1 + ref.length), "chr" -> chr, "length" -> length.toInt, "color" -> "blue")
    }

    val chr1 = query.map(x => x._1.head -> (x._2 + 1 + ref.length)).toMap
    val chr2 = ref.map(x => x._1.head -> (x._2 + 1)).toMap

    val row = lines.zipWithIndex.map { case (y, i) =>
      if (y.querySpecies == species) {
        Json.obj("bid" -> y.name, "color" -> "black", "id" -> (i + 1),
          "source" -> Json.obj("chr" -> chr2(y.query), "name" -> y.query, "start" -> y.queryStart, "end" -> y.queryEnd),
          "target" -> Json.obj("chr" -> chr1(y.ref), "name" -> y.ref, "start" -> y.refStart, "end" -> y.refEnd))
      } else {
        Json.obj("bid" -> y.name, "color" -> "black", "id" -> (i + 1),
          "source" -> Json.obj("chr" -> chr2(y.ref), "name" -> y.ref, "start" -> y.refStart, "end" -> y.refEnd),
          "target" -> Json.obj("chr" -> chr1(y.query), "name" -> y.query, "start" -> y.queryStart, "end" -> y.queryEnd))
      }
    }
    val collection = queryRow ++ refRow
    val table = lines.map(z => z.getMapByT() ++ Map("qSpecies" -> getTitle(z.querySpecies), "rSpecies" -> getTitle(z.refSpecies)))
    Json.obj("ur" -> row, "collection" -> collection, "table" -> table)
  }


  def getTitle(species: String): String = {
    TableUtils.genomeName(species)
  }

}
